package com.arcsoft.commonbase.ext

import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.DrawableRes
import androidx.recyclerview.widget.RecyclerView
import com.arcsoft.commonbase.R
import com.arcsoft.commonbase.databinding.LayoutEmptyViewBinding
import leifu.shapelibrary.ShapeView

fun Context.createEmptyView(emptyHint: String, @DrawableRes resId: Int = R.mipmap.ic_launcher): View {
    val view = LayoutInflater.from(this).inflate(R.layout.layout_empty_view, null)
    view.findViewById<TextView>(R.id.tv_empty_title).text = emptyHint
    view.findViewById<ImageView>(R.id.iv_def_empty_img).setImageResource(resId)
    return view

}

/**
 * 空view配置
 */
fun LayoutEmptyViewBinding?.setEmptyViewConfig(
    title: String = "暂无数据",
    subtitle: String = "暂无数据",
    @DrawableRes resId: Int = R.mipmap.ic_launcher,
//    emptyLayoutRootVisible: Boolean = false,
    btnRetryText: String = "刷新",
    retryButtonListener: (() -> Unit)? = null
) {
    this?.apply {
        tvEmptyTitle.text = title
        tvEmptySubTitle.text = subtitle

        ivDefEmptyImg.setImageResource(resId)
//        emptyLayoutRoot.visibleOrGone(emptyLayoutRootVisible)
        emptyLayoutRoot.visible()
        btnRetry.apply {
            setText(btnRetryText)
            if (btnRetryText == "刷新") {
                setTextColor(Color.parseColor("#666666"))
                setShapeStrokeColor(Color.parseColor("#DCDEE0"))
                    .setShapeSolidColor(Color.WHITE)
                    .setUseShape()

            }
            visibleOrGone(retryButtonListener != null)
            setOnClickListener {
                emptyLayoutRoot.gone()
                retryButtonListener?.invoke()
            }
        }
    }
}

fun RecyclerView.createEmptyView(
    emptyTitle: String,
    @DrawableRes resId: Int = R.mipmap.empty_default,
    emptySubTitle: String? = "",
    btnRetryText: String? = "",
    retryButtonListener: (() -> Unit)? = null
): View {
    val view = LayoutInflater.from(this.context).inflate(R.layout.layout_empty_view, this, false)
    view.findViewById<TextView>(R.id.tv_empty_title).text = emptyTitle
    view.findViewById<ImageView>(R.id.iv_def_empty_img).setImageResource(resId)
    view.findViewById<TextView>(R.id.tv_empty_sub_title).text = emptySubTitle
    view.findViewById<ShapeView>(R.id.btn_retry).apply {
        setText(btnRetryText)
        if (!btnRetryText.isNullOrEmpty()) {
            visible()
        }
        setOnClickListener {
            retryButtonListener?.invoke()
        }
    }

    return view
}
